<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Message Channel</title>
</head>
<body>
    <script>

const {port1, port2} = new MessageChannel();
//监听事件
//用DOM2级事件处理
port1.addEventListener('message', (msg) => {
	console.log('port1接收到信息：' + msg.data);
})
port2.addEventListener('message', (msg) => {
	console.log('port2接收到信息：' + msg.data);
})
//如果不调用start()方法的话，将监听不到message事件
port1.start();
port2.start();


port1.postMessage('来自port1的信息');
// port2.postMessage('来自port2的信息');

    </script>
</body>
</html>